package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.*;
import com.mes.biz.domain.vo.ProcessCompletionVo;
import com.mes.biz.service.*;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.domain.model.LoginUser;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import com.mes.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生产单Controller
 * @author hwg
 * @email hwg
 * @date 2023-03-30
 */
@RestController
@RequestMapping("/biz/bizProductionOrder")
public class BizProductionOrderController extends BaseController {

    @Autowired
    private BizProductionOrderService bizProductionOrderService;
    @Autowired
    private BizProductService bizProductService;
    @Autowired
    private BizOrderService bizOrderService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private BizProductProcessPlanService bizProductProcessPlanService;
    @Autowired
    private BizOrderProcessRecordService bizOrderProcessRecordService;

    /**
     * 查询生产工序列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizProductionOrder bizProductionOrder, HttpServletRequest request, HttpServletResponse response) {
        bizProductionOrder.setPage(new PageDomain(request, response));
        PageInfo<BizProductionOrder> page = bizProductionOrderService.findPage(bizProductionOrder);
        for (int i = 0; i < page.getList().size(); i++) {
            BizProduct bizProduct = bizProductService.get(page.getList().get(i).getProductId());
            page.getList().get(i).setProductCode(bizProduct.getProductCode());
            page.getList().get(i).setColor(bizProduct.getColor());
            page.getList().get(i).setSpec(bizProduct.getSpec());
            List<BizOrder> bizOrders = new ArrayList<>();
            BizOrder bizOrder = bizOrderService.get(page.getList().get(i).getProductOrderId());
            bizOrders.add(bizOrder);
            page.getList().get(i).setBizOrders(bizOrders);
        }
        return R.data(page);
    }

//    /**
//     * @Author: wy
//     * @Description:工序交接-商品明细
//     * @Params:
//     * @Return
//     */
//    @GetMapping("/getProductDetailList")
//    public R getProcessDetailList(BizProductionOrder bizProductionOrder, HttpServletRequest request, HttpServletResponse response) {
//        LoginUser loginUser = tokenService.getLoginUser(request);
////        BizOrder bizOrder = bizOrderService.get(bizProductionOrder.getProductOrderId());
//        List<BizProductionOrder> list = bizProductionOrderService.getProcessDetailList(bizProductionOrder.getProductOrderId(),loginUser.getUserId());
//        return R.data(list);
//    }

    /**
     * @Author: wy
     * @Description:工序交接-商品明细-工序进度
     * @Params:
     * @Return
     */
    @GetMapping("/getProductPlanDetail")
    public R getProductPlanDetail(BizProductionOrder bizProductionOrder, HttpServletRequest request, HttpServletResponse response) {
        List<BizProductionProcess> list = bizProductProcessPlanService.getCompletionByProductId(bizProductionOrder.getProductId());
//        boolean flag= false;
//        for (BizProductionProcess bizProductionProcess : list){
//            bizProductionOrder.setPlanProcessId(bizProductionProcess.getId());
//            //当前步骤是否完成
//            flag = bizOrderProcessRecordService.findInfo(bizProductionOrder);
//            bizProductionProcess.setFlag(flag);
//        }
        return R.data(list);
    }
}
